package com.zhiyou.controller.RecordController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhiyou.bean.House;
import com.zhiyou.bean.Logistics;
import com.zhiyou.bean.Record;
import com.zhiyou.service.RecordeService.RecordeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/record")
public class RecordController {

    @Autowired
    RecordeService recordeService;
    @RequestMapping("/list")
    public String getAll(@RequestParam(value = "pn",defaultValue = "1") Integer pn, Map<String, PageInfo> map){
        PageHelper.startPage(pn,5);
        List<Record> list = recordeService.getAll();
        PageInfo pageInfo = new PageInfo(list,5);
        map.put("pageInfo",pageInfo);

        return "recordlist";
    }


    @RequestMapping("/addrecord")
    public String add(Map<String,List> map){
        List<House> houses = recordeService.selectAddress();
        map.put("house",houses);
        List<Logistics> logistics = recordeService.selectName();
        map.put("logis",logistics);
        return "recordadd";
    }

    @RequestMapping("/add")
    public String addOne(Record r,String rtime,@RequestParam(value = "pn",defaultValue = "1") Integer pn, Map<String, PageInfo> map){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = dateFormat.parse(rtime);
            r.setReTime(date);

        } catch (ParseException e) {
            e.printStackTrace();
        }

        System.out.println(r);
        int i=recordeService.insert(r);
        if (i>=1){
            PageHelper.startPage(pn,5);
            List<Record> list = recordeService.getAll();
            PageInfo pageInfo = new PageInfo(list,5);
            map.put("pageInfo",pageInfo);
            return "recordlist";
        }
        return"recordadd";
    }
    @RequestMapping("/edit")
    public String editOne(Record record,Map<String,PageInfo> map,@RequestParam(value = "pn",defaultValue = "1") Integer pn){

        int updateOne = recordeService.updateOne(record);
        PageHelper.startPage(pn,5);
        List<Record> list = recordeService.getAll();
        PageInfo pageInfo = new PageInfo(list,5);
        map.put("pageInfo",pageInfo);
        return "recordlist";
    }
    @RequestMapping("/update")
    public String update(String reId,Map<String,Record> map){
        int id = Integer.parseInt(reId);
        Record record = recordeService.detail(id);
        map.put("record",record);

        return "recordedit";
    }

    @RequestMapping("/find")
    public String find(String reId,Map<String,Record> map){
        int id = Integer.parseInt(reId);
        Record record = recordeService.detail(id);
        map.put("record",record);

        return "recorddetial";
    }
    @RequestMapping("/delete")
    public String delete(String reId,Map<String,PageInfo> map,@RequestParam(value = "pn",defaultValue = "1") Integer pn){
        int delete = recordeService.delete(Integer.valueOf(reId));
        System.out.println(delete);
        PageHelper.startPage(pn,5);
        List<Record> list = recordeService.getAll();
        PageInfo pageInfo = new PageInfo(list,5);
        map.put("pageInfo",pageInfo);
        return "recordlist";
    }



    @RequestMapping("/like")
    public String selectlike(String feild, String name, Map<String,PageInfo> map, @RequestParam(value = "pn",defaultValue = "1") Integer pn, HttpSession session){
        Map<String,String> parMap = new HashMap<>();
        List<Record> list = null;
        if("hAddress".equals(feild)){
            parMap.put("hAddress",name);
        }else{
            parMap.put("loName",name);
        }
        if (name==""){
            session.removeAttribute("feild");
            session.removeAttribute("name");
            PageHelper.startPage(pn,5);
            list = recordeService.getAll();
            PageInfo pageInfo = new PageInfo(list,5);
            map.put("pageInfo",pageInfo);
            return "recordlist";
        }
        session.setAttribute("field",feild);
        session.setAttribute("name",name);
        PageHelper.startPage(pn,5);
        list = recordeService.selectLike(parMap);
        PageInfo pageInfo = new PageInfo(list,5);

        map.put("pageInfo1",pageInfo);

        return "recordlist2";



    }

}
